package ch2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AccountDaoInMemoryImpl implements AccountDao {
    private Map<Long,Account> accountsMap = new HashMap<>();
    {
        Account account1 = new Account();
        account1.setId(1L);
        account1.setOwnerName("John");
        account1.setBalance(10789.0);

        Account account2 = new Account();
        account2.setId(2L);
        account2.setOwnerName("Jack");
        account2.setBalance(20110.0);

        accountsMap.put(account1.getId(),account1);
        accountsMap.put(account2.getId(),account2);
    }
    @Override
    public void insert(Account account) {
        accountsMap.put(account.getId(),account);
    }

    @Override
    public void update(Account account) {
        accountsMap.replace(account.getId(),account);
    }

    @Override
    public void update(List<Account> account) {

    }

    @Override
    public void delete(long accountId) {
        accountsMap.remove(accountId);
    }

    @Override
    public Account find(long accountId) {
        return accountsMap.get(accountId);
    }

    @Override
    public List<Account> find(List<Long> accountIds) {
        List<Account> accounts = new ArrayList<>();
        for( Long accountId:accountIds) {
            Account account = accountsMap.get(accountId);
            if (account != null)
            {
                accounts.add(account);
            }
        }
        return accounts;
    }

    @Override
    public List<Account> find(String ownerName) {
        return null;
    }

    @Override
    public List<Account> find(boolean locked) {
        return null;
    }
}
